package com.hdrx.fundhelper.utils;

import org.apache.pdfbox.io.RandomAccessFile;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.File;

public class ReadF {

    public static void readFile(String filePath) throws Exception {

        File file = new File(filePath);
        System.out.println(">>>>>>>>>>>>>"+file);
        RandomAccessFile is = new RandomAccessFile(file, "r");
        PDFParser parser = new PDFParser(is);
        parser.parse();
        PDDocument doc = parser.getPDDocument();
        System.out.println("<<<<<<<<doc<<<<<<<<<<<<"+doc);
        PDFTextStripper textStripper = new PDFTextStripper();
        String s = textStripper.getText(doc).replaceAll(" ", "");
        System.out.println("++++++++"+s);
        //Document document = jta.getDocument();
        File file1 = new File(filePath);
        //count++;
        try {
            System.out.println("====================================start");
            System.out.println("姓 名：" + s.substring(s.indexOf("姓名"), s.indexOf("样本ID号")));
            String substring = s.substring(s.indexOf("样本ID号") + 5, s.indexOf("样本类型"));
            System.out.println("样本ID号：" + substring);


            String parent = file1.getParent() + "\\";
            String newName = parent + substring.trim() + ".pdf";



           /* document.insertString(document.getLength(), "==============第：" + count + "个文件start=================\n", null);
            document.insertString(document.getLength(), "姓 名：" + s.substring(s.indexOf("姓名") + 2, s.indexOf("样本ID号")) + "\n", null);
            document.insertString(document.getLength(), "样本ID号：" + substring + "\n", null);
            document.insertString(document.getLength(), "未修改：" + file1.getAbsolutePath() + "\n", null);
            document.insertString(document.getLength(), "修改为：" + newName + "\n", null);
            JScrollBar scrollBar = jsp.getVerticalScrollBar();    //得到竖直方向的滚动条
            scrollBar.setValue(scrollBar.getMaximum());        //设置滚动条为最大值

            System.out.println("未修改：" + file1);
            System.out.println("修改为：" + newName);
            doc.close();
            Boolean result = reNameFile(filePath, newName);
            document.insertString(document.getLength(), "修改结果：" + (result ? "修改成功" : "修改失败") + "\n", null);
            document.insertString(document.getLength(), "==============第：" + count + "个文件end==============\n", null);

            System.out.println("====================================end");*/
        } catch (Exception e) {
            //document.insertString(document.getLength(), file1.getAbsolutePath()+"文件模板格式发生改变，请自己手动修改一下\n", null);
        }finally {
            doc.close();
        }


    }

    public static void main(String[] args) throws Exception {
        String pdfPath = "E:/1/教育部学籍在线验证报告_侯东旭.pdf";
        readFile(pdfPath);
    }

}
